// pages/me/pages/savecompany/index.js
import { input, picker } from '../../../../utils/tools'
import { getoAuthToken, queryYezz } from '../../../../utils/bdutils'
import { http } from '../../../../utils/apiutils'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        form: {},
        step: 1,
        rule: [
            {
                key: 'factoryName',
                msg: '公司名称'
            },
            {
                key: 'business',
                msg: '行业类别'
            },
            {
                key: 'contactName',
                msg: '联系人'
            },
            {
                key: 'contactPhone',
                msg: '联系电话'
            },
            {
                key: 'creditCode',
                msg: '社会代码'
            },
            {
                key: 'legalName',
                msg: '法人代表'
            },
        ],
        range: [],
        show: false,// 控制支付弹窗显示
        typePayMoney:[
            {
                key:'宾馆酒店',
                money:100
            }
        ],
        needPayMoney:0, //需要缴费的金额
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getType();
        this.getComInfo();
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    input(e) {
        input.bind(this, e)();
    },
    picker(e) {
        picker.bind(this, e)();
    },
    choose() {
        let _this = this;
        wx.showLoading({ mask: true })
        wx.nextTick(_ => {
            wx.chooseImage({
                count: 1,
                success(res) {
                    var base64 = 'data:image/jpg;base64,' + wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64");
                    let params = {
                        image: base64
                    }
                    queryYezz(params, (res1) => {
                        _this.setData({
                            ['form.factoryName']: res1.words_result['单位名称'].words,
                            ['form.contactAddress']: res1.words_result['地址'].words,
                            ['form.legalName']: res1.words_result['法人'].words,
                            ['form.creditCode']: res1.words_result['社会信用代码'].words,
                        })
                        _this.addStep()
                        wx.hideLoading({
                            success: (res) => { },
                        })
                    }, err => {
                        wx.hideLoading({
                            success: (res) => { },
                        })
                    })
                    
                },
                fail(){
                    wx.hideLoading({
                        success: (res) => {},
                    })
                },  
            })
        })

    },
    addStep() {
        let _this = this;
        if (this.data.step == 1) {
            let firArr = ['factoryName', 'contactName', 'contactPhone', 'business'];
            this.data.rule.map(item => {
                if (firArr.includes(item.key) && !_this.data.form[item.key]) {
                    wx.showToast({
                        title: item.msg + '不能为空',
                        icon: 'none'
                    })
                    throw '另类return';
                }
            })
            getoAuthToken()
        }
        this.setData({
            step: this.data.step * 1 + 1
        })
    },
    submit() {
        wx.showLoading({})
        // debugger
        let _this = this;
        if (this.data.form.id) {
            console.log('我走了修改接口')
            http.companyOperation(this.data.form,
                function (res) {
                    wx.hideLoading()
                    wx.showModal({
                        title: "提示",
                        content: "保存成功",
                        showCancel: false,
                        success: function () {
                            getApp().globalData.exist=1;
                            getApp().globalData.userInfo.exist=1;
                            wx.navigateBack({
                                delta: 1,
                            })
                        },
                    })
                },
                function (err) {
                    wx.hideLoading()
                })
        } else {
            console.log('我走了新增接口')
            try {
                this.data.form.id = getApp().globalData.share_id || '';
                console.log('开始请求')
                console.log(this.data.form)
            http.addCompanyOperation(this.data.form, res => {
                console.log('请求成功')
                console.log(this.data.form)
                wx.showModal({
                    title: "提示",
                    content: "保存成功",
                    showCancel: false,
                    success: function () {
                        getApp().globalData.exist=1;
                        getApp().globalData.userInfo.exist=1;
                        let pageArr = getCurrentPages();
                        _this.getComInfo()
                        wx.nextTick(_=>{
                            if(pageArr[pageArr.length-1].route=='pages/table/home/index'){
                                wx.reLaunch({
                                  url: '../../../index/index',
                                })
                                return ;
                            }
                            wx.navigateBack({
                                delta: 1,
                            })
                        })
                        
                    },
                })
                wx.hideLoading({
                    success: (res) => { },
                })
            }, err => {
                this.data.form.id="";
                wx.hideLoading({
                    success: (res) => {
                        wx.showToast({
                          title: err,
                          icon:"none"
                        })
                     },
                })
            })
            } catch (error) {
               throw error;
            }
            
        }
    },
    getType() {
        http.getBusiness({}, res => {
            if (res.success) {
                this.setData({
                    range: res.data
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        }, err => {
            wx.showToast({
                title: err,
                icon: "none"
            })
        })
    },
    tijiao(){
        let isNeedPay=false;
        let needPayMoney=0;
        let _this =this;
        this.data.range.map(item=>{
            if(_this.data.form.business==item.name){
                // debugger
                if(item.vipPrice*1>0){
                    isNeedPay = true;
                    _this.data.needPayMoney=(item.vipPrice*1)||false;
                }
            }
        })
        if(isNeedPay&&!this.data.form.id){
            this.setData({ show: !this.data.show });
        }else{
            this.submit();
        }
    },
    changeshow() {
        this.setData({ show: !this.data.show })
    },
    pay() {
        let _this = this;
        http.createOrder('0/'+this.data.needPayMoney * 100, res => {
            wx.requestPayment({
                nonceStr: res.nonceStr,
                package: res.package,
                paySign: res.paySign,
                signType: res.signType,
                timeStamp: res.timeStamp,
                success(res) {
                    wx.showToast({
                        title: '充值成功',
                        icon: 'none',
                        success() {
                            // wx.navigateBack({
                            //     delta: 1,
                            // })
                            _this.submit()
                        }
                    })
                },
                fail(err) {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'none'
                    })
                }
            })
        }, err => {
            wx.showToast({
                title: err,
                icon: "none"
            })
        })
    },
    getComInfo: function () {
        var data = {

        }
        var that = this;
        wx.showLoading({
            title: '加载中...',
        })
        http.getComInfo(data,
            function (res) {
                wx.hideLoading()
                console.log("-----已完成任务列表---", res);
                if (res.success) {
                    if (res.data && res.data.id) {
                        that.setData({
                            form: {
                                id: res.data.id,
                                accountBank: res.data.accountBank,
                                accountName: res.data.accountName,
                                business: res.data.business,
                                contactAddress: res.data.factoryAddress,
                                contactName: res.data.contactName,
                                creditCode: res.data.creditCode,
                                enterTime: res.data.enterTime,
                                bankId: res.data.factoryBankId,
                                factoryId: res.data.factoryId,
                                factoryName: res.data.factoryName,
                                legalName: res.data.legalName,
                                contactPhone: res.data.legalPhone,
                                lat: res.data.lat,
                                lng: res.data.lng,
                            }
                        })
                        getApp().globalData.factoryInfo = res.data
                    }

                } else {
                    wx.showToast({
                        title: res.message,
                        icon: "none",
                    })
                }
            },
            function (err) {
                wx.hideLoading()
            })
    },
})